<?php
/**
 * Created by PhpStorm.
 * User: 潘潘
 * Date: 2021/12/19
 * Time: 20:45
 */
require_once '../tools/DbTools.php';

//获取输入的值
$code=$_POST['code'];
$phone=$_POST['phone'];
$name=$_POST['name'];
$password=$_POST['password'];

//判断
if(empty($code) || empty($phone) || empty($name) || empty($password)){
    echo '创建账户失败！'.'<a href="openAccount.php">返回开卡页面</a>';
    die;
}

//初始化数据库连接
DbTools::InitDb();
$sql="select * from accountinfo WHERE AccountCode='$code'";
$result=DbTools::select($sql);
$len = count($result);
if ($len==0){
    $sql="INSERT into accountinfo(AccountCode,AccountPhone,RealName,OpenTime) VALUES('$code','$phone','$name',NOW())";
    $result = DbTools::noSelect($sql);
    if (!$result["status"]){
        echo '创建账户失败！'.'<a href="openAccount.php">返回开卡页面</a>';
        die;
    }
}else{
    $realNameInDb = $result[0]["name"];
    if ($realName!=$realNameInDb){
        echo '真实姓名与账户不符！'.'<a href="openAccount.php">返回开卡页面</a>';
        die;
    }
}
$sql="select * from accountinfo WHERE AccountCode='$accountCode' and RealName='$realName'";
$res = DbTools::select($sql);
$accountId = $res[0]["AccountId"];
$cardNo="6";
for ($i=0;$i<12;$i++){
    $rand = rand(0, 9);
    $cardNo.=$rand;
}
//$cardPwd=md5($cardPwd);
$sql="INSERT into bankcard(CardNo,AccountId,CardPwd,CardMoney,CardState,CardTime) VALUES('$cardNo','$accountId','$cardPwd',0,0,NOW())";
$result = DbTools::noSelect($sql);
if (!$result["status"]){
    echo '开卡失败！'.'<a href="openAccount.php">返回开卡页面</a>';
    die;
}
echo "开卡成功！<a href='admin.php'>返回管理员</a>";

//关闭数据库
DbTools::close();
